package com.four.peoplemanage.gopolice.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.gopolice.dao.GopoliceDao;
import com.four.peoplemanage.gopolice.entity.Gopolice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GopoliceService {
    @Autowired
    GopoliceDao gopoliceDao;

    //申请出警
    public boolean insert(Gopolice gopolice){
        if (gopolice == null){
            return false;
        }
        gopolice.setStatus("待审核");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString = sdf.format(new Date());
        gopolice.setDispatch_time(dataTimeString);
        gopolice.setDispatch_date(dataTimeString);
        return gopoliceDao.addGoplice(gopolice)>0;
    }
    //分页
    public Page<Gopolice> getPageGopolice (int pageNum, int pageSize, String applicants, String department, String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Gopolice> list=gopoliceDao.getGopolice(start,pageSize,applicants,department,status);
        //获取总条目数
        int total=gopoliceDao.countGopolice(applicants,department,status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Gopolice> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //分页
    public Page<Gopolice> getOneGopolice (int pageNum, int pageSize, String applicants, String department,int infromation_id, String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Gopolice> list=gopoliceDao.getOneGopolice(start,pageSize,applicants,department,infromation_id,status);
        //获取总条目数
        int total=gopoliceDao.countGopolice(applicants,department,status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Gopolice> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //撤销
    public  boolean updateStatus(Gopolice gopolice){
        if (gopolice==null){
            return false;
        }
        gopolice.setStatus("已撤销");
        return gopoliceDao.updateGopolice(gopolice)>0;
    }

    //审核通过
    public  boolean updateSuccess(Gopolice gopolice){
        if (gopolice==null){
            return false;
        }
        gopolice.setStatus("已通过");
        return gopoliceDao.updateGopolice(gopolice)>0;
    }

    //审核驳回
    public  boolean updateFail(Gopolice gopolice){
        if (gopolice==null){
            return false;
        }
        gopolice.setStatus("已驳回");
        return gopoliceDao.updateGopolice(gopolice)>0;
    }

    //编辑
    public boolean updateGopolice(Gopolice gopolice){
        if (gopolice == null){
            return false;
        }
        gopolice.setStatus("待审核");
        return gopoliceDao.updateGopoliceById(gopolice)>0;
    }
}
